package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.applist.pojo.vo.ListAppVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.ChangeWorkflowDTO;
import com.xbongbong.pro.workorder.pojo.dto.ChangeWorkflowItemDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderCcGetDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderMyPendingDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderProgressDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderRelationTabGetDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderCcGetVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderChangeFlowItemVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderMyPendingVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderProcessVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.BusinessProductService;
import com.xbongbong.saas.service.SaasDetailTabService;
import com.xbongbong.saas.service.WorkOrderCcService;
import com.xbongbong.saas.service.WorkOrderFormDataService;
import com.xbongbong.saas.service.WorkOrderService;
import com.xbongbong.workorder.service.WorkOrderBasicService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/4/16 1:56 PM
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/workOrder/center")
public class MobileWorkOrderCenterController {

    @Resource
    private SaasDetailTabService saasDetailTabService;
    @Resource
    private WorkOrderFormDataService workOrderFormDataService;
    @Resource
    private BusinessProductService businessProductService;
    @Resource
    private WorkOrderService workOrderService;
    @Resource
    private WorkOrderCcService workOrderCcService;
    @Resource
    private WorkOrderBasicService workOrderBasicService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * Description: 工单进展
     * @param workOrderProgressDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/progress",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String progress(@RequestBody @Valid WorkOrderProgressDTO workOrderProgressDTO, BindingResult br) {
        XbbResponse<WorkOrderProcessVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderProcessVO workOrderProcessVO = workOrderFormDataService.progress(workOrderProgressDTO);
                response = new XbbResponse<>(workOrderProcessVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * Description: 工单流转弹窗
     * @param changeWorkflowItemDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/getChangeFlowItem",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getChangeFlowItem(@RequestBody @Valid ChangeWorkflowItemDTO changeWorkflowItemDTO, BindingResult br) {
        XbbResponse<WorkOrderChangeFlowItemVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderChangeFlowItemVO workOrderChangeFlowItemVO = workOrderService.getChangeFlowItem(changeWorkflowItemDTO);
                response = new XbbResponse<>(workOrderChangeFlowItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 工单进展
     * @param changeWorkflowDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/changeWorkflow",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeWorkflow(@RequestBody @Valid ChangeWorkflowDTO changeWorkflowDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(changeWorkflowDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(changeWorkflowDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }

                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), null, changeWorkflowDTO.getDistributorMark());
                BaseVO baseVO = new BaseVO();
                workOrderBasicService.changeWorkflow(changeWorkflowDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 获取工单抄送人员
     * @param workOrderCcGetDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/5/1 9:32
     * @since v1.0
     */
    @RequestMapping(value = "/getCc",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCc(@RequestBody @Valid WorkOrderCcGetDTO workOrderCcGetDTO, BindingResult br) {
        XbbResponse<WorkOrderCcGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderCcGetVO workOrderCcGetVO = workOrderCcService.getCc(workOrderCcGetDTO);
                response = new XbbResponse<>(workOrderCcGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 工单记录
     * @param workOrderRelationTabGetDTO
     * @param br
     * @author 刘阳
     * @date 2019/5/14 8:28 PM
     * @since v1.0
     */
    @RequestMapping(value = "/workOrderContract/get",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectList(@RequestBody @Valid WorkOrderRelationTabGetDTO workOrderRelationTabGetDTO, BindingResult br) {
        XbbResponse<ListAppVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ListAppVO listAppVO = saasDetailTabService.getWorkOrderContract(workOrderRelationTabGetDTO);
                response = new XbbResponse<>(listAppVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 首页获取待我处理（待接收和进行中）的工单列表卡片
     * @param workOrderMyPendingDTO
     * @return br
     * @author 刘阳
     * @date 2019/6/3 9:06 AM
     * @since v1.0
     */
    @RequestMapping(value = "/getMyPendingWorkOrderList",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMyPendingWorkOrderList(@RequestBody @Valid WorkOrderMyPendingDTO workOrderMyPendingDTO, BindingResult br) {
        XbbResponse<WorkOrderMyPendingVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), null, workOrderMyPendingDTO.getDistributorMark());
                WorkOrderMyPendingVO workOrderMyPendingVO = workOrderFormDataService.getMyPendingWorkOrderList(workOrderMyPendingDTO);
                response = new XbbResponse<>(workOrderMyPendingVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
